package com.xjm.test1.controller;


import com.xjm.test1.entity.MessageUserLoginRequest;
import com.xjm.test1.entity.MessageUserLoginResponse;
import com.xjm.test1.utils.ProtoBufUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.UUID;

@Controller
public class TestController {
//    @RequestMapping(value = "/demo/test", produces = "application/x-protobuf")
//    @ResponseBody
//    public MessageUserLogin.MessageUserLoginResponse getPersonProto(@RequestBody MessageUserLogin.MessageUserLoginRequest request) {
//        MessageUserLogin.MessageUserLoginResponse.Builder builder = MessageUserLogin.MessageUserLoginResponse.newBuilder();
//        builder.setAccessToken(UUID.randomUUID()+"_res");
//        builder.setUsername(request.getUsername()+"_res");
//
//        return builder.build();
//    }

//    @RequestMapping(value = "/demo/test", produces = "application/x-protobuf")
//    @ResponseBody
//    public byte[] getPersonProto(@RequestBody byte[] request) {
//        System.out.println("request =>" + Arrays.toString(request));
//        MessageUserLoginRequest requestObj = ProtoBufUtil.deserializer(request, MessageUserLoginRequest.class);
//
//        MessageUserLoginResponse response = new MessageUserLoginResponse();
//        response.setAccess_token(UUID.randomUUID()+"_res");
//        response.setUsername(requestObj.getUsername()+"_res");
//        System.out.println("response =>" + response);
//
//        return ProtoBufUtil.serializer(response);
//    }

    @PostMapping(value = "/demo/test")
    @ResponseBody
    public byte[] getPersonProto(@RequestBody byte[] request) {
        System.out.println("request =>" + Arrays.toString(request));
        MessageUserLoginRequest requestObj = ProtoBufUtil.deserializer(request, MessageUserLoginRequest.class);
        System.out.println("requestObj => " + requestObj);

        MessageUserLoginResponse response = new MessageUserLoginResponse();
        response.setAccessToken(UUID.randomUUID()+"_res");
        response.setUsername(requestObj.getUsername()+"_res");
        System.out.println("response =>" + response);

        byte[] serializer = ProtoBufUtil.serializer(response);
        System.out.println("response => " + Arrays.toString(serializer));

        return serializer;
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }

}
